﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace GIVisualEditor
{
    [Serializable()]
    public class DirectedGraph
    {
        private List<Node> nodes = new List<Node>();
        private List<Edge> edges = new List<Edge>();
        private bool isDirty = false;
        private List<Node> taskBag = new List<Node>();

        internal List<Node> TaskBag
        {
            get { return taskBag; }
        }

        public bool IsDirty
        {
            get { return isDirty; }
            set { isDirty = value; }
        }

        public List<Edge> Edges
        {
            get { return edges; }
            set { edges = value; }
        }


        public List<Node> Nodes
        {
            get { return nodes; }
            set { nodes = value; }
        }

        public DirectedGraph()
        {

        }

        public void Save(string sFilePath)
        {

        }


        public static DirectedGraph FromFile(string sFilePath)
        {

            return null;
        }

        public static void WriteNodeVale(string nodeName, string value, ref System.Xml.XmlWriter oWriter)
        {
            oWriter.WriteStartElement(nodeName);
            oWriter.WriteString(value);
            oWriter.WriteEndElement();
        }

        public void ReDraw()
        {

            // States dont' require repaint
            if (edges != null && edges.Count > 0)
            {
                foreach (Edge e in edges)
                {
                    e.View.Invalidate();
                }
            }
        }

        public string ToXML()
        {
            StringBuilder sbResult = new StringBuilder();
            int i = 0;
            System.Xml.XmlWriterSettings oSettings = new System.Xml.XmlWriterSettings();
            oSettings.Encoding = Encoding.UTF8;
            oSettings.OmitXmlDeclaration = true;
            oSettings.Indent = true;
            System.Xml.XmlWriter oWriter = System.Xml.XmlWriter.Create(sbResult, oSettings);
            //oWriter.Settings.Encoding = Encoding.UTF8;

            oWriter.WriteStartElement("GIVisualMachine");

            oWriter.WriteStartElement("Nodes");
            for(i=0; i < this.nodes.Count; i++)
            {
                this.nodes[i].ToXML(ref oWriter, i.ToString());
            }
            oWriter.WriteEndElement();

            oWriter.WriteStartElement("Edges");
            for (i = 0; i < this.edges.Count; i++)
            {
                this.edges[i].ToXML(ref oWriter);
            }
            oWriter.WriteEndElement();

            oWriter.WriteEndElement();

            oWriter.Close();

            return sbResult.ToString();

        }
       
    }
}
